package org.afun.oldhand.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import org.afun.oldhand.R;
import org.afun.oldhand.forum.javaBean.MealItemBean;
import org.afun.oldhand.home.activity.home_item1_1;
import org.afun.oldhand.home.activity.home_item1_2;
import org.afun.oldhand.home.activity.home_item1_3;
import org.afun.oldhand.home.activity.home_item1_4;
import org.afun.oldhand.home.activity.home_item1_5;
import org.afun.oldhand.home.activity.home_item1_6;
import org.afun.oldhand.home.javaBean.ChannelItemBean;

import java.util.ArrayList;
import java.util.List;

public class HomeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    // 三种类型
    public static final int CHANNEL = 0;      // 功能区
    public static final int BANNER = 1;       // 轮播图区
    public static final int MEAL = 2;         // 套餐区

    public int currentType = CHANNEL;
    private final LayoutInflater mLayoutInflater;


    public HomeRecyclerAdapter(Context mContext) {
        this.mContext = mContext;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    /**
     * 根据位置得到类型-系统调用
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case CHANNEL:
                currentType = CHANNEL;
                break;
            case BANNER:
                currentType = BANNER;
                break;
            case MEAL:
                currentType = MEAL;
                break;
            default:
                break;
        }
        return currentType;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == CHANNEL) {
            return new ChannelViewHolder(mLayoutInflater.inflate(R.layout.home_channel_item, null), mContext);
        } else if (viewType == BANNER) {
            return new BannerViewHolder(mLayoutInflater.inflate(R.layout.home_bannner_item, null), mContext);
        } else if(viewType==MEAL){
            return new MealViewHolder(mLayoutInflater.inflate(R.layout.home_meal_item,null),mContext);
        }

        return null;
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == CHANNEL) {
            ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder;
            channelViewHolder.setData();
        }else if(getItemViewType(position)==BANNER){
            BannerViewHolder bannerViewHolder=(BannerViewHolder) holder;
        }else if(getItemViewType(position)==MEAL){
            MealViewHolder mealViewHolder=(MealViewHolder) holder;
            mealViewHolder.setData();
        }
    }


    /**
     * 返回总条数，共三种类型
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return 3;
    }


    // ViewHolder区
    // 功能
    class ChannelViewHolder extends RecyclerView.ViewHolder{
        public GridView gvChannel;
        public Context mContext;
        private List<ChannelItemBean> data = new ArrayList<>();

        public ChannelViewHolder(View itemView, Context mContext) {
            super(itemView);
            gvChannel = (GridView) itemView.findViewById(R.id.gv_channel);
            this.mContext = mContext;
        }

        public void setData() {
            data.add(new ChannelItemBean( "教练匹配",R.drawable.home_item1_1));
            data.add(new ChannelItemBean( "时间预约",R.drawable.home_item1_2));
            data.add(new ChannelItemBean( "教练反馈",R.drawable.home_item1_3));
            data.add(new ChannelItemBean( "视频课程",R.drawable.home_item1_4));
            data.add(new ChannelItemBean( "模拟训练",R.drawable.home_item1_5));
            data.add(new ChannelItemBean( "更多",R.drawable.home_item1_6));
            ChannelAdapter channelAdapter = new ChannelAdapter(mContext, data);
            gvChannel.setAdapter(channelAdapter);


            // 设置点击事件
            gvChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent;
                    switch (position){
                        case 0:
                            intent=new Intent(mContext, home_item1_1.class);
                            mContext.startActivity(intent);
                            break;
                        case 1:
                            intent=new Intent(mContext, home_item1_2.class);
                            mContext.startActivity(intent);
                            break;
                        case 2:
                            intent=new Intent(mContext, home_item1_3.class);
                            mContext.startActivity(intent);
                            break;
                        case 3:
                            intent=new Intent(mContext, home_item1_4.class);
                            mContext.startActivity(intent);
                            break;
                        case 4:
                            intent=new Intent(mContext, home_item1_5.class);
                            mContext.startActivity(intent);
                             break;
                        case 5:
                            intent=new Intent(mContext, home_item1_6.class);
                            mContext.startActivity(intent);
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    }

    // 轮播图
    class BannerViewHolder extends RecyclerView.ViewHolder {
        private ViewPager viewPager;  //轮播图模块
        private int[] mImg;
        private int[] mImg_id;
        private String[] mDec;
        private ArrayList<ImageView> mImgList;
        private LinearLayout ll_dots_container;
        private TextView loop_dec;
        private int previousSelectedPosition = 0;
        boolean isRunning = false;
        public Context mContext;

        public BannerViewHolder(View itemView, Context mContext) {
            super(itemView);
            this.mContext = mContext;
            viewPager = itemView.findViewById(R.id.banner);
            ll_dots_container = itemView.findViewById(R.id.ll_dots_loop);
            loop_dec = itemView.findViewById(R.id.loop_dec);
            // 图片资源id数组

            mImg = new int[]{R.drawable.test3,
                    R.drawable.test3,
                    R.drawable.test3,
                    R.drawable.test3,
                    R.drawable.test3
            };
            //        // 文本描述
            mDec = new String[]{
                    "Test1",
                    "Test2",
                    "Test3",
                    "Test4",
                    "Test5"
            };

            mImg_id = new int[]{
                    R.id.pager_img1,
                    R.id.pager_img2,
                    R.id.pager_img3,
                    R.id.pager_img4,
                    R.id.pager_img5
            };

            // 初始化要展示的5个ImageView
            mImgList = new ArrayList<ImageView>();
            ImageView imageView;
            View dotView;
            LinearLayout.LayoutParams layoutParams;
            for (int i = 0; i < mImg.length; i++) {
                //初始化要显示的图片对象
                imageView = new ImageView(this.mContext);
                imageView.setBackgroundResource(mImg[i]);
                imageView.setId(mImg_id[i]);
                imageView.setOnClickListener(new pagerOnClickListener(this.mContext));
                mImgList.add(imageView);
                //加引导点
                dotView = new View(this.mContext);
                dotView.setBackgroundResource(R.drawable.dot);
                layoutParams = new LinearLayout.LayoutParams(10, 10);
                if (i != 0) {
                    layoutParams.leftMargin = 10;
                }
                //设置默认所有都不可用
                dotView.setEnabled(false);
                ll_dots_container.addView(dotView, layoutParams);
            }

            ll_dots_container.getChildAt(0).setEnabled(true);
            loop_dec.setText(mDec[0]);
            previousSelectedPosition = 0;
            //设置适配器
            viewPager.setAdapter(new LoopViewAdapter(mImgList));
            // 把ViewPager设置为默认选中Integer.MAX_VALUE / t2，从十几亿次开始轮播图片，达到无限循环目的;
            int m = (Integer.MAX_VALUE / 2) % mImgList.size();
            int currentPosition = Integer.MAX_VALUE / 2 - m;
            viewPager.setCurrentItem(currentPosition);

            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i1) {

                }

                @Override
                public void onPageSelected(int i) {
                    int newPosition = i % mImgList.size();
                    loop_dec.setText(mDec[newPosition]);
                    ll_dots_container.getChildAt(previousSelectedPosition).setEnabled(false);
                    ll_dots_container.getChildAt(newPosition).setEnabled(true);
                    previousSelectedPosition = newPosition;
                }

                @Override
                public void onPageScrollStateChanged(int i) {

                }
            });

            // 开启轮询
            new Thread() {
                public void run() {
                    isRunning = true;
                    while (isRunning) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        //下一条
                        ((Activity)mContext). runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
                            }
                        });
                    }
                }
            }.start();
        }


    }


    // 学车套餐
    class MealViewHolder extends RecyclerView.ViewHolder{
        public Context mContext;
        private RecyclerView recyclerView;
        private List<MealItemBean> data=new ArrayList<>();

        public MealViewHolder(@NonNull View itemView,Context context) {
            super(itemView);
            this.mContext = context;
            this.recyclerView=itemView.findViewById(R.id.home_meal);
        }

        public void setData(){
            data.add(new MealItemBean(null,"一对一全周班",null,null));
            data.add(new MealItemBean(null,"一对一全周班",null,null));
            data.add(new MealItemBean(null,"一对一全周班",null,null));
            data.add(new MealItemBean(null,"一对一平日班",null,null));
            data.add(new MealItemBean(null,"一对一平日班",null,null));
            data.add(new MealItemBean(null,"一对一平日班",null,null));

            // 设置线性布局
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.mContext);
            this.recyclerView.setLayoutManager(linearLayoutManager);

            this.recyclerView.setAdapter(new MealAdapter(mContext,data));
        }
    }

}